# 迭代器 __iter__ 返回一个迭代器对象
# 迭代器对象中有__next__方法(或 next()方法
# 内置函数 next(it) 等价于 it.__next__()

class Fibs:
    def __init__(self):
        self.a = 0
        self.b = 1

    def __next__(self):
        self.a, self.b = self.b, self.a + self.b
        return self.a

    def __iter__(self):
        return self


fib = Fibs()
for f in fib:
    if f > 1000:
        print(f)  # 1597
        break
#

it = iter([1, 2, 3])
print(next(it))
print(next(it))
print(next(it))


# print(next(it)) StopIteration


# 从迭代器创建队列

class TestIter:
    value = 0

    def __next__(self):
        self.value += 1
        if self.value > 10: raise StopIteration
        return self.value

    def __iter__(self):
        return self


ti = TestIter()
lst = list(ti)
print(lst)  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
